import logging
import os


LOG_DIR="logs"
os.makedirs(LOG_DIR, exist_ok=True)

#全局logger
formatter = logging.Formatter(
    "%(asctime)s [%(levelname)s] [%(name)s] %(message)s",
    datefmt="%Y-%m-%d %H:%M:%S"
)
def get_main_logger():
    """获取全局主 logger"""
    logger = logging.getLogger("main")
    if not logger.handlers:
        logger.setLevel(logging.DEBUG)
        fh = logging.FileHandler(os.path.join(LOG_DIR, "main.log"), encoding="utf-8")
        fh.setFormatter(formatter)
        logger.addHandler(fh)

        ch = logging.StreamHandler
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    return logger

def get_student_logger(student_id: str):
    """为某个学号单独生成 logger"""
    logger = logging.getLogger(f"student.{student_id}")
    if not logger.handlers:  # 避免重复绑定
        logger.setLevel(logging.DEBUG)
        fh = logging.FileHandler(os.path.join(LOG_DIR, f"{student_id}.log"), encoding="utf-8")
        fh.setFormatter(formatter)
        logger.addHandler(fh)
    return logger